home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / pbsv004 / pblib.c < prev    next >
C/C++ Source or Header  |  1993-08-05  |  3KB  |  197 lines

  1. /* pblib.c 1993.8.5 */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include "pbsv.h"
  9.  
  10. /*
  11.  * < initq > initialize queue
  12.  */
  13. VOID initq(q)
  14. struct stqueue *q;
  15. {
  16.     q->head = q->tail = NULL;
  17. }
  18.  
  19. /*
  20.  * < putq > put queue
  21.  */
  22. VOID putq(q,d)
  23. struct stqueue *q;
  24. struct stqcell *d;
  25. {
  26.     if (q == NULL || d == NULL) {
  27.         assert(0);
  28.     }
  29.     d->next = NULL;
  30.     if (q->head == NULL) {
  31.         q->head = q->tail = d;
  32.     } else {
  33.         q->tail->next = d;
  34.     q->tail = d;
  35.     }
  36. }
  37.  
  38. /*
  39.  * < getq > get queue
  40.  */
  41. struct stqcell *getq(q)
  42. struct stqueue *q;
  43. {
  44.     struct stqcell *r;
  45.  
  46.     if (q == NULL) {
  47.         assert(0);
  48.     }
  49.     r = q->head;
  50.     if (r != NULL) {
  51.         q->head = r->next;
  52.     }
  53.     if (q->head == NULL) {
  54.         q->tail = NULL;
  55.     }
  56.     return(r);
  57. }
  58.  
  59. /*
  60.  * < delq > delete queue
  61.  */
  62. VOID delq(q,d)
  63. struct stqueue *q;
  64. struct stqcell *d;
  65. {
  66.     struct stqcell *d2;
  67.     struct stqueue temp;
  68.  
  69.     initq(&temp);
  70.     while(d2 = getq(q)) {
  71.     if (d2 != d) {
  72.         putq(&temp,d2);
  73.     }
  74.     }
  75.     *q = temp;
  76. }
  77.  
  78. /*
  79.  * < addq > add queue
  80.  */
  81. VOID addq(q1,q2)
  82. struct stqueue *q1,*q2;
  83. {
  84.     struct stqcell *d;
  85.  
  86.     while(d = getq(q2))
  87.         putq(q1,d);
  88. }
  89.  
  90. /*
  91.  * < cntq > count queue
  92.  */
  93. int cntq(q)
  94. struct stqueue *q;
  95. {
  96.     struct stqcell *r;
  97.     int n;
  98.  
  99.     n = 0;
  100.     r = q->head;
  101.     while(r) {
  102.         r = r->next;
  103.     n++;
  104.     }
  105.     return(n);
  106. }
  107.  
  108. /*
  109.  * < ca2ad > call -> adrs
  110.  */
  111. VOID ca2ad(ca,ad)
  112. char *ca,ad[];
  113. {
  114.     int c,i;
  115.  
  116.     for (i = 0; i < 6; i++)
  117.         ad[i] = ' ' << 1;
  118.     ad[6] = 0x60;
  119.     for (i = 0; i < 7; i++) {
  120.         c = *ca++;
  121.     if (c == '\0') {
  122.         break;
  123.     } else if (isalpha(c) || isdigit(c)) {    /* A-Z, 0-9 */
  124.         ad[i] = c << 1;
  125.     } else if (c == '-') {
  126.         ad[6] |= atoi(ca) << 1;    /* ssid */
  127.         break;
  128.     }
  129.     }
  130. }
  131.  
  132. /*
  133.  * < ad2ca > adrs -> call
  134.  */
  135. VOID ad2ca(ad,ca)
  136. char ad[],*ca;
  137. {
  138.     char buf[4];
  139.     int c,i;
  140.  
  141.     for (i = 0; i < 6; i++) {
  142.         c = (ad[i] & 0xff) >> 1;
  143.     if (isalpha(c) || isdigit(c)) {
  144.         *ca++ = c;
  145.     } else
  146.         break;
  147.     }
  148.     *ca = '\0';
  149.     c = (ad[6] >> 1) & 0x0f;
  150.     if (c != 0) {
  151.     sprintf(buf,"-%d",c);
  152.     strcat(ca,buf);
  153.     }
  154. }
  155.  
  156. /*
  157.  * < ckcrc > check CRC
  158.  */
  159. ckcrc(buf,len)
  160. char buf[];
  161. int len;
  162. {
  163.     u_int gen_crc();
  164.  
  165.     u_int crc;
  166.     char *p;
  167.     int i;
  168.  
  169.     crc = 0;
  170.     p = &buf[17];
  171.     for(i = 17; i < len-2; i++)
  172.         crc = gen_crc(crc,*p++);
  173.     crc = gen_crc(crc, *p++);  /* 1st crc byte */
  174.     if (gen_crc(crc, *p)) {        /* 2nd crc byte */
  175.         printf("CRC Errors\n");
  176.     return(NG);
  177.     }
  178.     return(OK);
  179. }
  180.  
  181. u_int gen_crc(crc,data)
  182. u_int crc;
  183. u_char data;
  184. {
  185.     int y;
  186.  
  187.     crc ^= ((u_int)data) << 8;
  188.     for(y=0; y < 8; y++)
  189.         if( crc & 0x8000)
  190.             crc = crc << 1 ^ 0x1021;
  191.         else
  192.             crc <<= 1;
  193.     return(crc);
  194. }
  195.  
  196. /* pblib.c */
  197.